package com.nbjtjc.safe.mapper.edu;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.nbjtjc.safe.model.edu.EduDailyInfo;
import com.nbjtjc.safe.model.edu.EduDailyUserEvaluate;
import com.nbjtjc.util.MyMapper;
@Mapper
public interface EduDailyInfoMapper extends MyMapper<EduDailyInfo> {
	List<EduDailyInfo> search(@Param("edi")EduDailyInfo eduDailyInfo);
	
	List<EduDailyInfo> searchDetail(@Param("edi")EduDailyInfo eduDailyInfo);

	List<EduDailyInfo> managerFindDetail(@Param("edi")EduDailyInfo eduDailyInfo,@Param("parentComIds")List<Integer> parentComIds);

	List<EduDailyInfo> findByUser(@Param("edi")EduDailyInfo eduDailyInfo);
	List<EduDailyInfo> findDetailByUser(@Param("edi")EduDailyInfo eduDailyInfo);

	@Update("UPDATE edu_daily_info SET "
	+"evalue_count = (SELECT COUNT(1) FROM edu_daily_user_evaluate WHERE cancel = 0 and edu_daily_info_id = #{id} AND evaluate >=0 AND evaluate<=3),"
	+"evalue0_count = (SELECT COUNT(1) FROM edu_daily_user_evaluate WHERE cancel = 0 and edu_daily_info_id = #{id} AND evaluate = 0),"
	+"evalue1_count = (SELECT COUNT(1) FROM edu_daily_user_evaluate WHERE cancel = 0 and edu_daily_info_id = #{id} AND evaluate = 1),"
	+"evalue2_count = (SELECT COUNT(1) FROM edu_daily_user_evaluate WHERE cancel = 0 and edu_daily_info_id = #{id} AND evaluate = 2),"
	+"evalue3_count = (SELECT COUNT(1) FROM edu_daily_user_evaluate WHERE cancel = 0 and edu_daily_info_id = #{id} AND evaluate = 3)"
	+"WHERE cancel != 1 and id = #{id}")
	void updateCount(@Param("id") int id);
	
	@Update("update edu_daily_info set cancel = 1 where id = #{id}")
	int cancelById(@Param("id") int id);
	
    @Select("SELECT DISTINCT DATE_FORMAT(training_date,'%Y') AS 'year' FROM edu_daily_info where company_id = #{companyId} and cancel = 0 AND training_date IS NOT NULL")
    List<String> findYear(@Param("companyId")Integer companyId);

	int getFailPeople(@Param("startDate") Date startDate,@Param("endDate")Date endDate,@Param("compId") int compId);
	int getTotalPeople(@Param("startDate") Date startDate,@Param("endDate")Date endDate,@Param("compId") int compId);

	
	@Select(" SELECT IFNULL(SUM(training_hours),0)  FROM edu_daily_info a INNER JOIN edu_daily_participant b ON a.`id`=b.`edu_daily_info_id` "
			+ " WHERE a.cancel =0 AND a.status = 4 AND b.participate = 1 AND (b.cancel=0  OR b.cancel IS NULL) AND b.user_id = #{userId} ")
	int getUserTrainningHours(@Param("userId") int  userId);


}